package com.datastructure.algorithm.TwoOfNumberDevided;

public class Eliminate {

    public static void main(String[] args) {
        System.out.println(new Solution().divide(-2147483647,-2));
//        System.out.println(Integer.MIN_VALUE);
    }
}

class Solution {
    public int divide(int dividend, int divisor) {
        boolean flag = false;
        int result = 0;
        long dividend1=dividend;
        long divisor1 = divisor;
        if((dividend1<0&&divisor1>0)||(dividend1>0&&divisor1<0)){
            flag = true;
        }
        if(dividend1<=Integer.MIN_VALUE){
            if(divisor==1) {
                return Integer.MIN_VALUE;
            }else if(divisor==-1) {
                return Integer.MAX_VALUE;
            }
        }
        dividend1=Math.abs(dividend1);
        divisor1=Math.abs(divisor1);
        if (divisor1==1){
            if(dividend1>=Integer.MAX_VALUE){
                if(divisor==1) {
                    return Integer.MAX_VALUE;
                }else if(divisor==-1) {
                    return Integer.MIN_VALUE;
                }
            }
            return (flag)?((int) -dividend1):((int)dividend1);
        }
        if(dividend1<divisor1){
            return  0;
        }
        if(dividend1==divisor1){
            return (flag)?(-1):(1);
        }
        while (dividend1-divisor1>=0){
            dividend1-=divisor1;
            result++;
        }
        return (flag)?(-result):(result);
    }
}
